package org.jash.shopdemo2212;

import android.content.Intent;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.HashMap;

public class ShopActivity extends AppCompatActivity {
    private GridView gv;
    private GoodsAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);
        LinearLayout goods = findViewById(R.id.goods);
        for (int i = 0; i < goods.getChildCount(); i++) {
            LinearLayout child = (LinearLayout) goods.getChildAt(i);
            TextView tv = (TextView) child.getChildAt(2);
            tv.getPaint().setStrikeThruText(true);
        }
        gv = findViewById(R.id.gv);
        adapter = new GoodsAdapter();
        gv.setAdapter(adapter);
        HttpUtils.doGet("http://10.161.52.182:8080/goods/getAllGoods", new HashMap<String, Object>() {{
                    // 写入参数
            put("goods_category", 0);
                }}, GoodsEntity.class,
                r -> {
                    // 正确的处理
                    if (r.getCode() == 200) {
                        adapter.addAll(r.getData());
                    } else {
                        Toast.makeText(this, r.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }, t -> {
                    // 错误的处理
                    Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();
                });
        findViewById(R.id.nav_2).setOnClickListener(v -> {
            startActivity(new Intent(this, CategoryActivity.class));
        });
    }
}